package org.cn.leroy.controller;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.*;
import org.cn.leroy.entity.UserInfo;
import org.cn.leroy.service.UserInfoService;
import org.cn.leroy.util.PageTemplet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Leroy on 2016/11/7.
 */
@Controller
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    UserInfoService userInfoService;
    @RequestMapping(value = "/allInfo",method = RequestMethod.GET)
    @ResponseBody
    public List<UserInfo> queryAll(){
        List<UserInfo> list = userInfoService.queryAll();
        return list;
    }

    @RequestMapping(value = "/queryByPage",method = RequestMethod.POST)
    @ResponseBody
    public List<UserInfo>  queryByPage(@PathVariable("page") int page,@PathVariable("length") int length){
//        int cupage = Integer.valueOf(page);
//        int len = Integer.valueOf(length);
        List<UserInfo> result = userInfoService.selectByPage(page,length);
        return result;
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String insert(){
        return null;
    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public String update(){
        return null;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public String delete(){
        return null;
    }

    @RequestMapping("/importFile")
    @ResponseBody
    public String importFile(MultipartFile file,HttpServletRequest request){
        String path = request.getContextPath();
        System.out.println("path"+path);
        String basePath = request.getScheme()+"//"+request.getServerName()+":"+request.getServerPort()+path;
        System.out.println("basePath"+basePath);
        String fileName = null;
//        String allowSuffix = "";
        long allowSize = 20971520;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        try{
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
/*            int length = allowSuffix.indexOf(suffix);
                if(length == -1){
                    throw new Exception("请上传允许格式的文件");
                }*/
            System.out.println("fileNme"+fileName);
            if(file.getSize() > allowSize){
                throw new Exception("您上传的文件大小已经超出范围");
            }
            String realPath = request.getSession().getServletContext().getRealPath("/");
            File upFile = new File(realPath);
            if (!upFile.exists()){
                upFile.mkdirs();
            }
            String fileNameNew = simpleDateFormat.format(new Date())+"."+suffix;
            File f = new File(upFile.getAbsoluteFile()+"/"+fileNameNew);
            file.transferTo(f);
            f.createNewFile();
            fileName = basePath+fileNameNew;
            String excelPath = realPath+fileNameNew;
            System.out.println("excel "+excelPath);

        }catch (Exception e){
            e.getStackTrace();
        }
        return "success";
    }
    /**
     * 按名字生成文件夹
     * @param name
     * @return
     */
    public static int[] hashname(String name){
        int value;
        int index;
        int len=name.length();
        int[] res=new int[3];
        byte[] name_byte=name.getBytes();

        value=name_byte[0]+name_byte[len-1];
        res[0]=Math.abs(value%64);
        value=10081;
        for(index=0;index<len;index++)
            value=(value+(value<<5))^name_byte[index];
        res[1]=Math.abs(value%64);
        value=10081;
        for(index=len-1;index>=0;index--)
            value=(value+(value<<6))^name_byte[index];
        res[2]=Math.abs(value%64);
        return res;
    }
}
